//
//  PywAlertView.m
//  PywSdk
//
//  Created by Jade on 4/8/16.
//  Copyright © 2016 zero. All rights reserved.
//

#import "PywAlertView.h"
#import "UIImage+Pyw.h"
#import "PywMasonry.h"

@interface PywAlertView()<UIAlertViewDelegate>
@property (nonatomic,strong) UIView *mainView;
@property (nonatomic,strong) UILabel *topTitleView;
@property (nonatomic,strong) UIImageView *iconImgView;
@property (nonatomic,strong) UILabel *messageLabel;
@property (nonatomic,strong) UILabel *subMessageLabel;
@property (nonatomic,strong) UIButton *cancelButton;
@property (nonatomic,strong) UIButton *sureButton;


@property (nonatomic,copy) NSString *title;
@property (nonatomic,copy) NSString *message;
@property (nonatomic,copy) NSString *subMessage;
@property (nonatomic,copy) NSString *messageImage;
@property (nonatomic) MessageAlignment messageAlignment;
@property (nonatomic,copy) NSString *cancelButtonTitle;
@property (nonatomic,copy) NSString *sureButtonTitle;
@end
@implementation PywAlertView
const CGFloat fontSize = 14.f;
const CGFloat subMessageFontSize = 11.f;

- (instancetype)initWithTitle:(NSString *)title
                      message:(NSString *)message
                   subMessage:(NSString *)subMessage
                 messageImage:(NSString *)messageImage
             messageAlignment:(MessageAlignment)messageAlignment
                     delegate:(id<PywAlertViewDelegate>)delegate
            cancelButtonTitle:(NSString *)cancelButtonTitle
            sureButtonTitle:(NSString *)sureButtonTitle
{
    self = [super init];
    if (self) {
        self.title = title;
        self.message = message;
        self.subMessage = subMessage;
        self.messageImage = messageImage;
        self.messageAlignment = messageAlignment;
        self.delegate = delegate;
        self.cancelButtonTitle = cancelButtonTitle;
        self.sureButtonTitle = sureButtonTitle;
        [self setUp];
    }
    return self;
}

#pragma mark - setUp
- (void)setUp
{
    self.backgroundColor = [UIColor colorWithWhite:0. alpha:.6];
    [self addSubview:self.mainView];
    [self.mainView addSubview:self.topTitleView];
    [self.mainView addSubview:self.messageLabel];
    [self.mainView addSubview:self.iconImgView];
    [self.mainView addSubview:self.subMessageLabel];
    [self.mainView addSubview:self.cancelButton];
    [self.mainView addSubview:self.sureButton];
    [self addConstraints];
}

/**
 *  添加约束
 */
- (void)addConstraints
{
    __weak typeof(self) weakSelf = self;
    [self.mainView mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.center.offset(0);
        if (IS_IPHONE) {
            make.width.equalTo(@(SCREEN_MIN_LENGTH * 0.9));
        } else {
            make.width.equalTo(@370);
        }
    }];
    
    [self.topTitleView mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.left.top.offset(0);
        make.right.equalTo(weakSelf.mainView.mas_right);
        make.height.mas_equalTo(40.f);
    }];
    
    [self.iconImgView mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.top.equalTo(weakSelf.topTitleView.mas_bottom).offset(10);
        make.size.mas_equalTo(CGSizeMake(60.f, 60.f));
        if (weakSelf.messageAlignment == MessageAlignmentVertical && weakSelf.message.length == 0) {
            make.centerX.offset(0);
        }else {
            make.left.offset(30.f);
        }
    }];

    [self.messageLabel mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.left.equalTo(weakSelf.iconImgView.mas_right).offset(10.f);
        make.right.equalTo(weakSelf.mainView.mas_right).offset(-10);
        switch (weakSelf.messageAlignment) {
            case MessageAlignmentVertical:
            {
                make.centerY.equalTo(weakSelf.iconImgView.mas_centerY);
            }
                break;
            case MessageAlignmentHorizontal:
            {
                if (weakSelf.subMessage.length == 0) {
                    make.centerY.equalTo(weakSelf.iconImgView.mas_centerY);
                }else {
                    make.top.equalTo(weakSelf.iconImgView.mas_top).offset(15);
                }
                
            }
                break;
            default:
                break;
        }

    }];
    
    [self.subMessageLabel mas_makeConstraints:^(PywMASConstraintMaker *make) {
        switch (weakSelf.messageAlignment) {
            case MessageAlignmentVertical:
            {
                make.centerX.offset(0);
                make.width.equalTo(weakSelf.mainView.mas_width).multipliedBy(0.8);
                make.top.equalTo(weakSelf.iconImgView.mas_bottom).offset(10.f);
            }
                break;
            case MessageAlignmentHorizontal:
            {
                make.left.equalTo(weakSelf.messageLabel.mas_left);
                make.top.equalTo(weakSelf.messageLabel.mas_bottom).offset(5);
                make.right.equalTo(weakSelf.mainView.mas_right).offset(-10);
            }
                break;
            default:
                break;
        }
    }];
    
    [self.cancelButton mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.left.offset(10);
        make.bottom.equalTo(weakSelf.mainView.mas_bottom).offset(-20).priorityHigh;
        make.height.mas_equalTo(40.f);
        switch (weakSelf.messageAlignment) {
            case MessageAlignmentVertical:
            {
                make.top.equalTo(weakSelf.subMessageLabel.mas_bottom).offset(10);
            }
                break;
            case MessageAlignmentHorizontal:
            {
                make.top.equalTo(weakSelf.subMessageLabel.mas_bottom).offset(25);
            }
                break;
            default:
                break;
        }
    }];
    
    [self.sureButton mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.right.offset(-10);
        make.left.equalTo(weakSelf.cancelButton.mas_right).offset(10);
        make.top.equalTo(weakSelf.cancelButton.mas_top);
        make.width.equalTo(weakSelf.cancelButton.mas_width);
        make.height.equalTo(weakSelf.cancelButton.mas_height);
        make.bottom.equalTo(weakSelf.cancelButton.mas_bottom);
    }];
    
}


#pragma mark - getter
- (UIView *)mainView
{
    if (!_mainView) {
        _mainView = [[UIView alloc] init];
        _mainView.backgroundColor = [UIColor whiteColor];
        _mainView.layer.cornerRadius = 20.f;
        _mainView.layer.masksToBounds = YES;
    }
    return _mainView;
}

- (UILabel *)topTitleView
{
    if (!_topTitleView) {
        _topTitleView = [UILabel new];
        _topTitleView.font = [UIFont systemFontOfSize:fontSize];
        _topTitleView.textAlignment = NSTextAlignmentCenter;
        _topTitleView.text = self.title;
        _topTitleView.backgroundColor = [UIColor colorWithRed:250/255.f
                                                        green:250/255.f
                                                         blue:250/255.f
                                                        alpha:1.f];
    }
    return _topTitleView;
}

- (UILabel *)messageLabel
{
    if (!_messageLabel) {
        _messageLabel = [UILabel new];
        _messageLabel.font = [UIFont systemFontOfSize:fontSize];
        _messageLabel.text = self.message;
        _messageLabel.numberOfLines = 4;
    }
    return _messageLabel;
}

- (UILabel *)subMessageLabel
{
    if (!_subMessageLabel) {
        _subMessageLabel = [UILabel new];
        _subMessageLabel.font = [UIFont systemFontOfSize:subMessageFontSize];
        _subMessageLabel.numberOfLines = 3;
        _subMessageLabel.textColor = [UIColor colorWithRed:115/255.f
                                                     green:115/255.f
                                                      blue:115/255.f
                                                     alpha:1.f];
        _subMessageLabel.text = self.subMessage;
    }
    return _subMessageLabel;
}

- (UIImageView *)iconImgView
{
    if (!_iconImgView) {
        _iconImgView = [[UIImageView alloc] initWithImage:
                        [UIImage imageWithName:self.messageImage]];
    }
    return _iconImgView;
}

- (UIButton *)cancelButton
{
    if (!_cancelButton) {
        _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _cancelButton.layer.cornerRadius = 20;
        _cancelButton.titleLabel.font = [UIFont systemFontOfSize:fontSize];
        [_cancelButton setTitle:self.cancelButtonTitle
                           forState:UIControlStateNormal];
        [_cancelButton setTitleColor:[UIColor colorWithRed:87/255.f
                                                     green:87/255.f
                                                      blue:87/255.f
                                                     alpha:1.f]
                                forState:UIControlStateNormal];
        _cancelButton.backgroundColor = [UIColor whiteColor];
        [_cancelButton setTitleColor:[UIColor blueColor]
                                forState:UIControlStateHighlighted];
        _cancelButton.layer.borderWidth = 1;
        _cancelButton.layer.borderColor = [UIColor colorWithRed:233/255.f
                                                          green:233/255.f
                                                           blue:233/255.f
                                                          alpha:1.f].CGColor;
        [_cancelButton addTarget:self
                              action:@selector(selectButton:)
                    forControlEvents:UIControlEventTouchUpInside];
        [_cancelButton setAccessibilityIdentifier:@"alert_common_cancelbtn"];
    }
    return _cancelButton;
}

- (UIButton *)sureButton
{
    if (!_sureButton) {
        _sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _sureButton.layer.cornerRadius = 20;
        _sureButton.titleLabel.font = [UIFont systemFontOfSize:fontSize];
        _sureButton.tag = 1;
        [_sureButton setTitle:self.sureButtonTitle
                     forState:UIControlStateNormal];
        _sureButton.backgroundColor = [UIColor colorWithRed:255/255.f
                                                      green:188/255.f
                                                       blue:0/255.f
                                                      alpha:1.f];
        [_sureButton setTitleColor:[UIColor blackColor]
                           forState:UIControlStateHighlighted];
        [_sureButton setTitleColor:[UIColor whiteColor]
                           forState:UIControlStateNormal];
        [_sureButton addTarget:self
                         action:@selector(selectButton:)
               forControlEvents:UIControlEventTouchUpInside];
        [_sureButton setAccessibilityIdentifier:@"alert_common_surebtn"];
    }
    return _sureButton;
}


#pragma mark - Public
/**
 *  显示提示框
 */
- (void)show
{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [self mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.left.top.right.bottom.offset(0);
    }];

}

/**
 *  添加到指定的view上
 */
- (void)show:(UIView *)view
{
    [view addSubview:self];
    [self mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.left.top.right.bottom.offset(0);
    }];
}

/**
 *  隐藏提示框
 */
- (void)hide
{
    [self removeFromSuperview];
}

#pragma mark - Private
/**
 *  点击按钮
 *
 *  @param sender <#sender description#>
 */
- (void)selectButton:(UIButton *)sender
{
    
    NSInteger index = sender.tag;
    if (index == 0) {
        if ([self.delegate respondsToSelector:@selector(alertViewCancel:)]) {
            [self.delegate alertViewCancel:self];
        }
    }
    if ([self.delegate respondsToSelector:@selector(alertView:clickedButtonAtIndex:)]) {
        [self.delegate alertView:self clickedButtonAtIndex:index];
    }
    
    [self hide];
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
}

@end
